package com.wzh.project.asm.main;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;

import com.css.sword.kernel.platform.component.utils.org.objectweb.asm.ClassWriter;
import com.css.sword. kernel.platform.component.utils.org.objectweb.asm.MethodVisitor;
import com.css.sword.kernel.platform.component.utils.org.objectweb.asm.Opcodes;
import com.css.sword.kernel.platform.component.utils.org.objectweb.asm.util.TraceClassVisitor;

public class Test implements Opcodes{

	public static void main(String[] args) throws Exception  {
		 
		ClassWriter cw = new ClassWriter(0);
		PrintWriter printWriter = new PrintWriter(System.out);
		TraceClassVisitor visitor = new TraceClassVisitor(cw, printWriter);
	 
		visitor.visit(V1_5, ACC_PUBLIC, "com/css/sword/esb/bean/auto/ReqBean", null, "java/lang/Object", null);
		
		
		//添加构造方法
		MethodVisitor mv = visitor.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
		mv.visitInsn(RETURN);
		mv.visitMaxs(1, 1);
		mv.visitEnd();
		
		visitor.visitField(ACC_PRIVATE, "test", "Ljava/lang/String;", null, null);
	 
		// 添加get方法
		mv = visitor.visitMethod(ACC_PUBLIC, "getTest", "()Ljava/lang/String", null, null);
		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0); //from object onto the stack
		mv.visitFieldInsn(GETFIELD, 
		  "com/css/sword/esb/bean/auto/ReqBean;", //the type of object
		  "test", // field name 
		 "Ljava/lang/String;" // field type
		);
		mv.visitInsn(ARETURN);
		mv.visitEnd();
		
		MethodVisitor mv1 = visitor.visitMethod(ACC_PUBLIC, "setTest", "(Ljava/lang/String;)V", null, null);
		mv1.visitCode();
		mv1.visitVarInsn(ALOAD, 0);//push the object 
		mv1.visitVarInsn(ALOAD, 1);//push the value from which assign to the field
		mv1.visitFieldInsn(PUTFIELD, 
		"com/css/sword/esb/bean/auto/ReqBean;", // the class
		"test", // the field name
		"Ljava/lang/String;"//the field type
		);
		mv1.visitEnd();
		
	 
		File file=new File("D:\\ReqBean.class");
		if (!file.exists()) {
			System.out.println("不存在，进行创建");
		}
		FileOutputStream fos = new FileOutputStream(file);
		fos.write(cw.toByteArray());
		fos.close();

		 System.out.println("end");
		
	}

}
